import 'dart:convert';

import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/net/api/medicine_apis.dart';
import 'package:ls_work/net/response/medicine_advice/drug_list_data.dart';
import 'package:ls_work/net/response/medicine_advice/medicine_elder_list_data.dart';
import 'package:visitor_scan_package/visitor_scan_package.dart';

import '../net/api/medicine_advice_api.dart';
import '../net/response/medicine_advice/medical_prescroption_list_data.dart';
import '../net/response/medicine_advice/medicine_advice_list_data.dart';
import '../net/response/medicine_advice/medicine_handover_list_data.dart';
import '../net/response/medicine_advice/medicine_lack_list_data.dart';
import '../net/response/medicine_advice/put_medicine_list_data.dart';
import '../net/response/medicine_advice/receive_medicine_record_list.dart';
import '../net/response/medicine_advice/take_medicine_list_data.dart';
import '../net/response/medicine_advice/treatment_detail_list_data.dart';
import '../net/response/medicine_advice/treatment_record_list_data.dart';

class MedicineAdviceModel extends BaseMvvmModel {
  @override
  void onCleared() {}

  var jsonHead = Options(contentType: Headers.jsonContentType);
  var urlEncodeHead = Options(contentType: Headers.formUrlEncodedContentType);

  Future<List<MedicalPrescroptionListData>> prescriptionList(
      {String? prescriptionNoLike,
      String? customerNameLike,
      String? bedNoLike,
      String? treatmentCardLike,
      String? doctorLike,
      String? prescriptionDateLike,
      int page = 1,
      int rows = 20}) async {
    var map = {
      "prescriptionNoLike": prescriptionNoLike,
      "customerNameLike": customerNameLike,
      "bedNoLike": bedNoLike,
      "treatmentCardLike": treatmentCardLike,
      "doctorLike": doctorLike,
      "prescriptionDateLike": prescriptionDateLike,
      "page": page,
      "rows": rows,
      "isApp": 1
    };
    var data = await HttpGo.instance
        .postData(MedicineApis.prescriptionList, data: map, options: jsonHead);
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?) ?? [])
          .map((e) => MedicalPrescroptionListData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  Future<List<TreatmentRecordListData>> treatmentRecord(
      {String? prescriptionNoLike,
      String? customerNameLike,
      String? bedNoLike,
      String? treatmentCardLike,
      String? doctorLike,
      String? prescriptionDateLike,
      String? projectNameLike,
      int page = 1,
      int rows = 20,
      dynamic status}) async {
    var map = {
      "prescriptionNoLike": prescriptionNoLike,
      "customerNameLike": customerNameLike,
      "bedNoLike": bedNoLike,
      "treatmentCardLike": treatmentCardLike,
      "doctorLike": doctorLike,
      "prescriptionDateLike": prescriptionDateLike,
      "projectNameLike": projectNameLike,
      "page": page,
      "rows": rows,
      "status": status,
    };
    var data = await HttpGo.instance
        .postData(MedicineApis.treatmentList, data: map, options: jsonHead);
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?) ?? [])
          .map((e) => TreatmentRecordListData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///治疗执行
  Future<Map> treatmentApply(dynamic id) async {
    var map = {"id": id};
    var data = await HttpGo.instance.postData(MedicineApis.treatmentApply,
        data: map,
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///治疗详情
  Future<List<TreatmentDetailListData>> treatmentDetailList(
      dynamic recordId) async {
    var map = {"recordId": recordId};
    var data = await HttpGo.instance
        .postData(MedicineApis.treatmentDetail, data: map, options: jsonHead);
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?) ?? [])
          .map((e) => TreatmentDetailListData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///缺药列表
  Future<List<MedicineLackListData>> medicineLackList(
      {String? customerNameLike,
      String? bedNoLike,
      String? drugNameLike,
      String? lackDateLike,
      dynamic status,
      int page = 1,
      int rows = 20}) async {
    var map = {
      "customerNameLike": customerNameLike,
      "bedNoLike": bedNoLike,
      "drugNameLike": drugNameLike,
      "lackDateLike": lackDateLike,
      "status": status,
      "page": page,
      "rows": rows,
    };
    var data = await HttpGo.instance
        .postData(MedicineApis.medicineLackList, data: map, options: jsonHead);
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?) ?? [])
          .map((e) => MedicineLackListData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///收药入柜列表
  Future<List<ReceiveMedicineRecordList>> receiveMedicineRecord(
      {String? customerNameLike,
      String? bedNoLike,
      String? drugNameLike,
      dynamic bizType,
      dynamic status,
      int page = 1,
      int rows = 20}) async {
    var map = {
      "customerNameLike": customerNameLike,
      "bedNoLike": bedNoLike,
      "drugNameLike": drugNameLike,
      "bizType": bizType,
      "status": status,
      "page": page,
      "rows": rows,
      "isInList":1
    };
    var data = await HttpGo.instance.postData(
        MedicineApis.receiveMedicineRecord,
        data: map,
        options: jsonHead);
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?) ?? [])
          .map((e) => ReceiveMedicineRecordList.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///收药入柜执行
  Future<Map> receiveMedicine(List<dynamic> flowId) async {
    var map = {"flowIds": flowId};
    var data = await HttpGo.instance
        .postData(MedicineApis.receiveMedicine, data: map, options: jsonHead);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///长者药柜列表
  Future<List<MedicineElderListData>> medicineElder(
      {String? customerNameLike,
      String? bedNoLike,
      int page = 1,
      int rows = 20}) async {
    var map = {
      "customerNameLike": customerNameLike,
      "bedNoLike": bedNoLike,
      "page": page,
      "rows": rows,
    };
    var data = await HttpGo.instance
        .postData(MedicineApis.medicineElderList, data: map, options: jsonHead);
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?) ?? [])
          .map((e) => MedicineElderListData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///药品列表
  Future<List<DrugListData>> drugListData({String? commonNameLike}) async {
    var map = {"commonNameLike": commonNameLike};
    var data = await HttpGo.instance
        .postData(MedicineApis.drugList, data: map, options: jsonHead);
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?) ?? [])
          .map((e) => DrugListData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///计算最小包装数量
  Future<num> calculateMinNum(
      {required dynamic drugId,
       String? maxNum ,
       String? minNum }) async {
    var map = {
      "drugId": drugId,
      "maxNum": maxNum.isNullOrEmpty()?"0":maxNum,
      "mixNum": minNum.isNullOrEmpty()?"0":minNum,
    };
    var data = await HttpGo.instance.postData(MedicineApis.calculateMinNum,
        queryParameters: map, options: urlEncodeHead);
    if ("0" == data['code']) {
      return data['data'] as num;
    } else {
      throw data['msg'];
    }
  }

  ///新增药品入库
  Future<Map> submitReceiveMedicine(
      {dynamic customerId,
      dynamic stockId,
      dynamic drugId,
      dynamic quantity}) async {
    var map = {
      "customerId": customerId,
      "stockId": stockId,
      "drugId": drugId,
      "quantity": quantity,
    };
    var data = await HttpGo.instance.postData(
        MedicineApis.receiveMedicineSubmit,
        data: map,
        options: jsonHead);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///药品移交列表
  Future<List<MedicineHandoverListData>> medicineHandoverList(
      {String? customerNameLike,
      String? bedNoLike,
      String? drugNameLike,
      String? executorTimeLike,
      String? executorNameLike,
      int page = 1,
      int rows = 20}) async {
    var map = {
      "customerNameLike": customerNameLike,
      "bedNoLike": bedNoLike,
      "drugNameLike": drugNameLike,
      "executorTimeLike": executorTimeLike,
      "executorNameLike": executorNameLike,
      "page": page,
      "rows": rows,
    };
    var data = await HttpGo.instance.postData(MedicineApis.medicineHandoverList,
        data: map, options: jsonHead);
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?) ?? [])
          .map((e) => MedicineHandoverListData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///药品移交提交
  Future<Map> submitMedicineHandover(
      {required dynamic stockId,
      required dynamic drugId,
      required dynamic transferQuantity,
      String? transferReason,
      int isTransfer = 0}) async {
    var map = {
      "stockId": stockId,
      "drugId": drugId,
      "transferQuantity": transferQuantity,
      "transferReason": transferReason,
      "isTransfer": isTransfer,
    };
    var data = await HttpGo.instance.postData(
        MedicineApis.submitMedicineHandover,
        data: map,
        options: jsonHead);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///摆药列表
  Future<List<PutMedicineListData>> putMedicineList(
      {String? customerNameLike,
      String? bedNoLike,
      dynamic floorId,
      dynamic buildingId,
      String? drugNameLike,
      String? planTimeLike,
      dynamic status,
      int page = 0,
      int rows = 20}) async {
    var map = {
      "customerNameLike": customerNameLike,
      "bedNoLike": bedNoLike,
      "floorId": floorId,
      "buildingId": buildingId,
      "drugNameLike": drugNameLike,
      "planTimeLike": planTimeLike,
      "status": status,
      "page": page,
      "rows": rows,
    };
    var data = await HttpGo.instance
        .postData(MedicineApis.putMedicineList, data: map, options: jsonHead);
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?) ?? [])
          .map((e) => PutMedicineListData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///摆药执行核对
  Future<Map> checkPutMedicine(
      {required List<dynamic> ids, dynamic type}) async {
    var map = {"ids": ids, "type": type};
    var data = await HttpGo.instance
        .postData(MedicineApis.checkPutMedicine, data: map, options: jsonHead);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///作废摆药
  Future<Map> cancelPutMedicine(dynamic id) async {
    var map = {"id": id};
    var data = await HttpGo.instance.postData(MedicineApis.cancelPutMedicine,
        data: map, options: urlEncodeHead);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///新增摆药
  Future<Map> submitPutMedicine({
    dynamic stockId,
    dynamic drugId,
    dynamic usageQuantity,
    dynamic jsonBean,
  }) async {
    var map = {
      "stockId": stockId,
      "drugId": drugId,
      "usageQuantity": usageQuantity,
      "usageMethod": jsonBean,
    };
    var data = await HttpGo.instance
        .postData(MedicineApis.addPutMedicine, data: map, options: jsonHead);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///服药列表
  Future<List<TakeMedicineListData>> takeMedicineList(
      {String? customerNameLike,
      String? bedNoLike,
      dynamic floorId,
      dynamic buildingId,
      String? drugNameLike,
      String? planTimeLike,
      dynamic status,
      int page = 0,
      int rows = 20,
      dynamic usageTime}) async {
    var map = {
      "customerNameLike": customerNameLike,
      "bedNoLike": bedNoLike,
      "floorId": floorId,
      "buildingId": buildingId,
      "drugNameLike": drugNameLike,
      "planTimeLike": planTimeLike,
      "status": status,
      "page": page,
      "rows": rows,
      "usageTime": usageTime,
    };
    var data = await HttpGo.instance
        .postData(MedicineApis.takeMedicineList, data: map, options: jsonHead);
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?) ?? [])
          .map((e) => TakeMedicineListData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///摆药执行核对
  Future<Map> checkTakeMedicine(
      {required List<dynamic> ids, dynamic type}) async {
    var map = {"ids": ids, "type": type};
    var data = await HttpGo.instance
        .postData(MedicineApis.checkTakeMedicine, data: map, options: jsonHead);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///作废摆药
  Future<Map> cancelTakeMedicine(dynamic id) async {
    var map = {"id": id};
    var data = await HttpGo.instance.postData(MedicineApis.cancelTakeMedicine,
        data: map, options: urlEncodeHead);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///新增服药
  Future<Map> addTakeMedicine({
    dynamic stockId,
    dynamic drugId,
    dynamic usageQuantity,
    dynamic usageTime,
    String? planTime
}) async {
    var map = {
      "stockId":stockId,
      "drugId":drugId,
      "usageQuantity":usageQuantity,
      "usageTime":usageTime,
      "planTime":planTime,
    };
    var data = await HttpGo.instance.postData(MedicineApis.addTakeMedicine,data: map,options: jsonHead);
    if("0"==data['code']){
      return data;
    }else{
      throw data['msg'];
    }
  }


  ///医嘱列表
  Future<List<MedicineAdviceListData>> medicineAdviceList({
    String? customerNameOrBedNoLike,
    String? startDate,
    String? endDate,
    String? createDate,
    dynamic ordersConfirm,
    dynamic ordersCheck,
    dynamic typeId,
    int page = 1,
    int rows = 20
  }) async {
    var map = {
      "customerNameOrBedNoLike":customerNameOrBedNoLike,
      "startDate":startDate,
      "endDate":endDate,
      "createDate":createDate,
      "ordersConfirm":ordersConfirm,
      "ordersCheck":ordersCheck,
      "typeId":typeId,
      "page":page,
      "rows":rows,
    };
    var data = await HttpGo.instance.postData(MedicineAdviceApi.medicineAdviceList,data: map,options: Options(contentType: Headers.jsonContentType));
    if("0"==data['code']){
      return ((data['data']['rows'] as List?)??[]).map((e) => MedicineAdviceListData.fromJson(e)).toList();
    }else{
      throw data['msg'];
    }
  }

  Future<Map> adviceConfirm(dynamic id) async {
    var map = {
      "id":id
    };
    var data = await HttpGo.instance.postData(MedicineAdviceApi.medicineAdviceConfirm,data: map,options: Options(contentType: Headers.formUrlEncodedContentType));
    if("0"==data['code']){
      return data;
    }else{
      throw data['msg'];
    }
  }

  Future<Map> adviceCheck(dynamic id) async {
    var map = {
      "id":id
    };
    var data = await HttpGo.instance.postData(MedicineAdviceApi.medicineAdviceCheck,data: map,options: Options(contentType: Headers.formUrlEncodedContentType));
    if("0"==data['code']){
      return data;
    }else{
      throw data['msg'];
    }
  }
}
